package descentFly
{
	import UIFree.ObjectEvent;
	
	import descentFly.data.df_fields;
	import descentFly.sound.df_soundManager;
	
	import flash.events.Event;
	import flash.events.IEventDispatcher;
	
	public class df_optionsObject extends df_spaceObject {
		public static const SETOPTIONS: String = 'SETOPTIONS';
		
		private var _options: Object;
		public function df_optionsObject(a_options: Object) {
			super();
			if (a_options != null) setOptions(a_options);
		}
		
		public function get id(): int {
			return (options.hasOwnProperty(df_fields.FIELD_ID))?options[df_fields.FIELD_ID]:0;
		}
		
		private function parseExtends(Extends: Object): void {
			for (var prop: String in Extends) {
				var method: String = 'parse_' + prop;
				if (this.hasOwnProperty(method)) this[method](Extends[prop]);
				else throw('method ' + method + ' not found');
			}
		}
		
		protected function checkOption(optionName: String, defaultValue: Object): void {
			if (!options.hasOwnProperty(optionName)) options[optionName] = defaultValue;
		}
		
		public function playSound(soundName: String, ref: Object=null): void {
			if (options && options.sounds && options.sounds.hasOwnProperty(soundName))
				playSoundFromObject(options.sounds[soundName], ref);
		}
		
		protected function playSoundFromObject(soundObj: Object, ref: Object=null): void {
			df_soundManager.playSound(soundObj, ref);
		}
		
		public function get options(): Object {
			return _options;
		}
		
		protected function setOptions(a_options: Object): void {
			_options = a_options;
			if (_options.hasOwnProperty(df_fields.FIELD_EXTENDS)) 
				parseExtends(_options.Extends);
			
			dispatchEvent(new Event(SETOPTIONS));
		}
	}
}